package hubol;

import static bogus.core.BogusCore.egl;
import owg.util.euclidian.Compass;
import owg.util.opengl.ColorF;
import owg.util.opengl.Tex;
import owg.util.opengl.TextureLib;

public class Sprite {
    public final Tex texture;
    public final short length;
    Compass origin = Compass.CENTER;

    public Sprite(String s){
    	this(TextureLib.tex(s));
    }
    
    public Sprite(String s, Compass origin){
    	this(TextureLib.tex(s), origin);
    }

    public Sprite(Tex t){
		this(t, Compass.CENTER);
    }
    
    public Sprite(Tex t, Compass origin){
    	if(t == null)
		    System.err.println("Warning: Creating Sprite for non-existing texture");
		texture = t;
		length = t.length;
		this.origin = origin;
    }

    public void setOrigin(Compass c){
	origin = c;
    }

    public void render(double imageSingle, double x, double y){
	render(imageSingle, x, y, 1, 1, 0, null);
    }

    public void render(double imageSingle, double x, double y, ColorF color){
	render(imageSingle, x, y, 1, 1, 0, color);
    }

    public void render(double imageSingle, double x, double y, float xscale, float yscale, float angle){
	render(imageSingle, x, y, xscale, yscale, angle, null);
    }

    public void render(double imageSingle, double x, double y, float xscale, float yscale, float angle, ColorF color){
	if (color != null)
	    egl.setColor(color);

	egl.enableTexture2D(texture, (int)imageSingle);
	egl.modelviewMatrix().push();
	egl.modelviewMatrix().translatef((float)x, (float)y, 0);
	egl.modelviewMatrix().rotatef(-angle, 0, 0, 1);
	egl.modelviewMatrix().scalef(xscale * texture.width, yscale * texture.height ,1);
	egl.modelviewMatrix().translatef(-origin.dx, -origin.dy, 0);
	
	if(texture.xMult != 1 || texture.yMult != 1)
	{
	    egl.textureMatrix().push();
	    egl.textureMatrix().scalef(texture.xMult, texture.yMult, 1);
	}
	
	egl.nwSquare.render();
	
	if(texture.xMult != 1 || texture.yMult != 1)
	    egl.textureMatrix().pop();
	
	egl.modelviewMatrix().pop();
    }

}
